/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.awt.Image;
import java.net.URL;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * Car class to manage car symbol character and for a future functional enhancement.
 * @author Chiaki Sato
 * @since J2SDK 1.6
 * @version 1.0
 */
public class Car {

    // private instance valiables
    private Image oImageCarHoriz;
    private Image oImageCarVert;

    /**
     * Constructor for the car class, no process.
     */
    public Car(){

        URL oPath;

        try{

            ImageIcon oIcon;
            String stPath;

            Random oRandom = new Random();

            // select a car image randomly
            int nGifNum = oRandom.nextInt(Const.CAR_TYPES_NUM) +1;

            // load the horizontal car image
            stPath = "/images/car" + nGifNum +"_h.GIF";
            oPath = getClass().getResource(stPath);
            if (oPath != null){
                oIcon = new ImageIcon(oPath);
                oImageCarHoriz = oIcon.getImage();

            }
            else{
                JOptionPane.showMessageDialog(null, Const.ERR_MSG_LOAD_OF_RESOURCE + stPath, Const.ERR_TITLE_IN_CLASS + getClass().getName(), JOptionPane.ERROR_MESSAGE);
            }

            // load the vertical car image
            stPath = "/images/car" + nGifNum +"_v.GIF";
            oPath = getClass().getResource(stPath);
            if (oPath != null){
                oIcon = new ImageIcon(oPath);
                oImageCarVert = oIcon.getImage();

            }
            else{
                JOptionPane.showMessageDialog(null, Const.ERR_MSG_LOAD_OF_RESOURCE + stPath, Const.ERR_TITLE_IN_CLASS + getClass().getName(), JOptionPane.ERROR_MESSAGE);
            }

        }
        catch (Exception e){

            JOptionPane.showMessageDialog(null, e, Const.ERR_TITLE_IN_CLASS + getClass().getName(), JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * Override function of toString to return a car symbol string.
     * @return Car symbol string
     */
    @Override
    public String toString(){
        return Const.SYMBOL_CAR;
    }

    public Image getImageCarHoriz() {
        return oImageCarHoriz;
    }

    public Image getImageCarVert() {
        return oImageCarVert;
    }
}
